/***************************************************************************
 * 
 * Copyright (c) 2015 Baidu.com, Inc. All Rights Reserved
 * $Id$ 
 * 
 **************************************************************************/
 
 /**
 * @file solution.h
 * @author liutongren(liutongren@baidu.com)
 * @date 2015/06/15 13:13:22
 * @version $Revision$ 
 * @brief 
 *  
 **/
#ifndef SOLUTION_H
#define SOLUTION_H
#include <vector>
#include <algorithm>
#include <utility>
using std::pair;
using std::vector;

class Solution {
public:
    bool int2pair(const vector<int>& nums, vector<std::pair<int, int> >* result) {
        int size_nums = nums.size();
        if (nums.size() > 0 && result != NULL) {
            for (int i =0; i != size_nums; ++i) {
                pair<int, int> index_value(i+1, nums[i]);
                result->push_back(index_value);
            }
            return true;
        } else {
            return false;
        }
    }
    static bool inline pair_greater(pair<int, int> a, pair<int, int> b) {
        return a.second < b.second;
    } 
    vector<int> twoSum(vector<int>& nums, int target) {
        int len_vec = nums.size();
        vector<int> vec;
        vector<pair<int, int> > nums_pair; 
        if (len_vec != 0) {
            int2pair(nums, &nums_pair);
            sort(nums_pair.begin(), nums_pair.end(), pair_greater); 
            int front_index = 0;
            int back_index = len_vec -1;
            while (nums_pair[front_index].second + nums_pair[back_index].second != target) {
                if (nums_pair[front_index].second + nums_pair[back_index].second < target) {
                    ++front_index;
                } else if (nums_pair[front_index].second + nums_pair[back_index].second > target) {
                    --back_index;
                } else {

                }
                if (back_index < front_index) {
                    break;
                }
            }
            if (nums_pair[front_index].second + nums_pair[back_index].second == target) {
                vec.push_back(std::min(nums_pair[front_index].first, nums_pair[back_index].first));
                vec.push_back(std::max(nums_pair[front_index].first, nums_pair[back_index].first));
            }
        } else {
            return vec;
        }
    }
};
#endif  // SOLUTION_H
#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::endl;
int main(int argc, char* argv[]) {
    Solution solu;
    //int list[] = {3, 2, 4};
    int list[] = {0, 3, 2, 4, 0};
    vector<int> nums(list, list + sizeof(list)/sizeof(int));
    vector<int> result = solu.twoSum(nums, 0);
    cout << "index1" << result[0] << endl;
    cout << "index2" << result[1] << endl;

    return 0;
}
/* vim: set ts=4 sw=4 sts=4 tw=100 */
